// pages/cart/cart.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEdit:true,
    edtText:'编辑',
    isSelAll:false,
    list:[],
    totalNum:0,
    totalJifen:0,
    loading: false,
    tip: '上拉加载',
    page: 1,
    is_pull: true,
    triggered: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _self = this;
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: ((res.windowHeight * 750) / res.windowWidth)
        })
      }
    });
    _self.getList();

  },
  getList() {
    let _self = this;
    if (_self.data.is_pull === false) {
      return;
    }
    _self.setData({
      loading: true,
      tip: '正在加载',
    });

    app.http.$ajax({
      url: 'v1.shop/cart',
      data: {
        page: _self.data.page
      },
      loading: false
    }, {
        success(res) {
          let data = res.data.data;
          if(_self.data.page == 1) {
            _self.data.list = data;
          } else {
            _self.data.list.push(...data);
          }
          _self.setData({
            list: _self.data.list
          });

          _self.data.page++;
        },
        complete(res) {
          _self.setData({
            triggered: false
          })
          app.http.complete(_self, res, app, 15);
        }
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  bindEdit:function(){
    let _self = this;
    var edtBtnText='';
    if(_self.data.isEdit){
      edtBtnText='完成'
    }else{
      edtBtnText='编辑'

    }
    _self.setData({
      isEdit : !_self.data.isEdit,
      edtText:edtBtnText
    });
  },
  selAll:function(){
    let _self = this;
    _self.setData({
      isSelAll : !_self.data.isSelAll
    })
    // console.log(_self.data.isSelAll);
    var curList = _self.data.list;
    if(_self.data.isSelAll){
      curList.forEach(function(item, index){
        curList[index].isselect = true;
      })
      _self.setData({
        list:curList
      });
      _self.sumAll();
    }else{
      curList.forEach(function(item, index){
        curList[index].isselect = false;
      });
      _self.setData({
        list:curList
      })
      _self.sumAll();
    }
  },
  selItem:function(e){
    let _self = this;
    var selId = e.currentTarget.id;
    var curlist =  _self.data.list;
    curlist.forEach(function(item, index){
      if(index == selId){
        curlist[index].isselect = !curlist[index].isselect
      }      
    });
    if(curlist[selId].isselect){
      var selAllItem = true;
      curlist.forEach(function(item, index){
        if(!curlist[index].isselect){
          selAllItem = false;
        }
      });
      _self.setData({
        isSelAll:selAllItem
      })
    }
    else{
      _self.setData({
        isSelAll:false
      })
    }
    _self.setData({
      list:curlist
    })
    _self.sumAll();
  },
  jia:function(e){
    let _self = this;
    var selId = e.currentTarget.id;
    var curlist =  _self.data.list;
    curlist.forEach(function(item, index){ 
      if(index == selId){
        curlist[index].num = curlist[index].num +1
      }
    })
    console.log('22222ffff')
    _self.setData({
      list:curlist
    })
    _self.sumAll();
  },
  jian:function(e){
    let _self = this;
    var selId = e.currentTarget.id;
    var curlist =  _self.data.list;
    curlist.forEach(function(item, index){
      if(index == selId){
        if(curlist[index].num <= 1){    
         return;
        } else {
          curlist[index].num = curlist[index].num - 1
        }
      }
    })
    _self.setData({
      list:curlist
    })
    _self.sumAll();
  },
  sumAll:function(){
    let _self = this;
    let sumNum = 0;
    let sumJifen = 0;
    var curList = _self.data.list;
    curList.forEach(function(item, index){
      if(item.isselect){
        sumNum = sumNum + item.num;
        sumJifen = sumJifen + item.num * item.price
      }
    })
    //console.log(sumNum +'========'+ sumJifen)
    _self.setData({
      totalNum:sumNum,
      totalJifen:sumJifen
    })
  },
  jieSuan:function(){
    let _self = this;
    var dataArr =  _self.data.list;
    var data={};
    var ids = '';
    dataArr.forEach(function(item, index){
      if (item.isselect === true) {
        if(index === 0) {
          ids = item.id;
        } else {
          ids += ',' + item.id;
        }

        var key = 'number_' + item.id
        data[key] = item.num;
      }
    })
    data.ids = ids;
    app.http.$ajax({
      url: 'v1.shop/createOrder',
      data: data
    }, {
      success(res) {
        app.success(res.msg);
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/order/order?orderId=' + res.data,
          })
        }, 1000);
      }
    });
  },
  delgoods() {
    let _self = this;
    var dataArr = _self.data.list;
    var data = {};
    var ids = '';
    dataArr.forEach(function (item, index) {
      if (item.isselect === true) {
        if (!ids) {
          ids = item.id;
        } else {
          ids += ',' + item.id;
        }
      }
    })
    if(!ids) {
      app.error('请选择要移除的商品');
      return;
    }
    data.ids = ids;
    app.http.$ajax({
      url: 'v1.shop/delete',
      data: data,
      loading: '正在删除'
    }, {
        success(res) {
          ids += '';
          let delArr = ids.split(',');
          let list = _self.data.list;
          let arr = list.filter(item => !delArr.includes(item.id + ''));
          console.log(arr);
          _self.setData({
            list: arr
          });
          app.success(res.msg);
        }
      });
  },
  down() {
    let _self = this;
    _self.data.page = 1;
    _self.data.is_pull = true;
    this.getList();
  },
 
})